package com.shop.cereshop.agent.controller.cart;

import com.shop.cereshop.agent.page.product.CartVO;
import com.shop.cereshop.agent.page.user.CereAgentUserVO;
import com.shop.cereshop.agent.param.product.AgentCartDTO;
import com.shop.cereshop.agent.param.product.CartDeleteDTO;
import com.shop.cereshop.agent.param.product.AgentCartSelectDTO;
import com.shop.cereshop.agent.service.product.AgentCartService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.agent.CereAgentRole;
import com.shop.cereshop.commons.domain.agent.CereAgentUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RequestMapping("/cart")
@RestController
@Api(value = "CereAgentCartController", tags = "购物车管理")
public class AgentCartController {

  @Autowired
  private AgentCartService cartService;

  // 新增购物车
  @ApiOperation(value = "添加商品到购物车接口")
  @PostMapping("/add")
  public Result<Void> addCart(@RequestBody @Valid AgentCartDTO agentCartDTO, HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    Long agentUserId = user.getAgentUserId();
    agentCartDTO.setAgentUserId(agentUserId);
    Integer number = cartService.addCart(agentCartDTO);
    if (number > 0) {
      return Result.success();
    }
    return Result.failed(CoReturnFormat.FAIL, "添加失败");
  }

  @ApiOperation(value = "批量删除购物车列表接口")
  @PostMapping("/delete")
  public Result<Void> deleteCart(@RequestBody CartDeleteDTO cartDeleteDTO, HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    Integer number = cartService.deleteCart(cartDeleteDTO.getCartIds(), user.getAgentUserId());
    if (number > 0) {
      return Result.success();
    }
    return Result.failed(CoReturnFormat.FAIL, "删除失败");
  }

  @ApiOperation(value = "查询购物车列表")
  @PostMapping("/list")
  public Result<Page<CartVO>> listCart(@RequestBody AgentCartSelectDTO agentCartSelectDTO, HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    Long agentUserId = user.getAgentUserId();
    agentCartSelectDTO.setAgentUserId(agentUserId);
    return Result.success(cartService.listCart(agentCartSelectDTO));
  }

  @ApiOperation(value = "更新购物车列表接口")
  @PostMapping("/update")
  public Result<Void> updateCart(@RequestBody AgentCartDTO agentCartDTO, HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    Long agentUserId = user.getAgentUserId();
    agentCartDTO.setAgentUserId(agentUserId);
    Integer number = cartService.updateCart(agentCartDTO);
    if (number > 0) {
      return Result.success();
    }
    return Result.failed(CoReturnFormat.FAIL, "更新失败");
  }
}
